(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('three'), require('@photo-sphere-viewer/core'), require('@photo-sphere-viewer/settings-plugin')) :
    typeof define === 'function' && define.amd ? define(['exports', 'three', '@photo-sphere-viewer/core', '@photo-sphere-viewer/settings-plugin'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.PhotoSphereViewer = global.PhotoSphereViewer || {}, global.PhotoSphereViewer.ResolutionPlugin = {}), global.THREE, global.PhotoSphereViewer, global.PhotoSphereViewer.SettingsPlugin));
})(this, (function (exports, THREE, PhotoSphereViewer, SettingsPlugin) {

/*!
 * PhotoSphereViewer.ResolutionPlugin 5.4.4
 * @copyright 2023 Damien "Mistic" Sorel
 * @licence MIT (https://opensource.org/licenses/MIT)
 */
"use strict";
  var __defProp = Object.defineProperty;
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  var __getOwnPropNames = Object.getOwnPropertyNames;
  var __hasOwnProp = Object.prototype.hasOwnProperty;
  var __export = (target, all) => {
    for (var name in all)
      __defProp(target, name, { get: all[name], enumerable: true });
  };
  var __copyProps = (to, from, except, desc) => {
    if (from && typeof from === "object" || typeof from === "function") {
      for (let key of __getOwnPropNames(from))
        if (!__hasOwnProp.call(to, key) && key !== except)
          __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
    }
    return to;
  };

  // @photo-sphere-viewer/core
  var require_core = () => PhotoSphereViewer;

  // src/index.ts
  var src_exports = {};
  __export(src_exports, {
    ResolutionPlugin: () => ResolutionPlugin,
    events: () => events_exports
  });
  var import_core3 = require_core();

  // src/events.ts
  var events_exports = {};
  __export(events_exports, {
    ResolutionChangedEvent: () => ResolutionChangedEvent
  });
  var import_core = require_core();
  var _ResolutionChangedEvent = class _ResolutionChangedEvent extends import_core.TypedEvent {
    constructor(resolutionId) {
      super(_ResolutionChangedEvent.type);
      this.resolutionId = resolutionId;
    }
  };
  _ResolutionChangedEvent.type = "resolution-changed";
  var ResolutionChangedEvent = _ResolutionChangedEvent;

  // src/ResolutionPlugin.ts
  var import_core2 = require_core();
  var getConfig = import_core2.utils.getConfigParser({
    resolutions: null,
    defaultResolution: null,
    showBadge: true
  });
  var _ResolutionPlugin = class _ResolutionPlugin extends import_core2.AbstractPlugin {
    constructor(viewer, config) {
      super(viewer);
      this.resolutions = [];
      this.resolutionsById = {};
      this.state = {
        resolution: null
      };
      this.config = getConfig(config);
      if (this.config.defaultResolution && this.viewer.config.panorama) {
        import_core2.utils.logWarn(
          "ResolutionPlugin, a defaultResolution was provided but a panorama is already configured on the viewer, the defaultResolution will be ignored."
        );
      }
    }
    /**
     * @internal
     */
    init() {
      super.init();
      this.settings = this.viewer.getPlugin("settings");
      if (!this.settings) {
        throw new import_core2.PSVError("Resolution plugin requires the Settings plugin");
      }
      this.settings.addSetting({
        id: _ResolutionPlugin.id,
        type: "options",
        label: this.viewer.config.lang.resolution,
        current: () => this.state.resolution,
        options: () => this.resolutions,
        apply: (resolution) => this.__setResolutionIfExists(resolution),
        badge: !this.config.showBadge ? null : () => this.state.resolution
      });
      this.viewer.addEventListener(import_core2.events.PanoramaLoadedEvent.type, this);
      if (this.config.resolutions) {
        this.setResolutions(
          this.config.resolutions,
          this.viewer.config.panorama ? null : this.config.defaultResolution
        );
        delete this.config.resolutions;
        delete this.config.defaultResolution;
      }
    }
    /**
     * @internal
     */
    destroy() {
      this.viewer.removeEventListener(import_core2.events.PanoramaLoadedEvent.type, this);
      this.settings.removeSetting(_ResolutionPlugin.id);
      super.destroy();
    }
    /**
     * @internal
     */
    handleEvent(e) {
      if (e instanceof import_core2.events.PanoramaLoadedEvent) {
        this.__refreshResolution();
      }
    }
    /**
     * Changes the available resolutions
     * @param resolutions
     * @param defaultResolution - if not provided, the current panorama is kept
     * @throws {@link PSVError} if the configuration is invalid
     */
    setResolutions(resolutions, defaultResolution) {
      this.resolutions = resolutions || [];
      this.resolutionsById = {};
      resolutions.forEach((resolution) => {
        if (!resolution.id) {
          throw new import_core2.PSVError("Missing resolution id");
        }
        if (!resolution.panorama) {
          throw new import_core2.PSVError("Missing resolution panorama");
        }
        this.resolutionsById[resolution.id] = resolution;
      });
      if (!this.viewer.config.panorama && !defaultResolution) {
        defaultResolution = resolutions[0].id;
      }
      if (defaultResolution && !this.resolutionsById[defaultResolution]) {
        import_core2.utils.logWarn(`Resolution ${defaultResolution} unknown`);
        defaultResolution = resolutions[0].id;
      }
      if (defaultResolution) {
        this.setResolution(defaultResolution);
      }
      this.__refreshResolution();
    }
    /**
     * Changes the current resolution
     * @throws {@link PSVError} if the resolution does not exist
     */
    setResolution(id) {
      if (!this.resolutionsById[id]) {
        throw new import_core2.PSVError(`Resolution ${id} unknown`);
      }
      return this.__setResolutionIfExists(id);
    }
    __setResolutionIfExists(id) {
      if (this.resolutionsById[id]) {
        return this.viewer.setPanorama(this.resolutionsById[id].panorama, { transition: false, showLoader: false });
      } else {
        return Promise.resolve();
      }
    }
    /**
     * Returns the current resolution
     */
    getResolution() {
      return this.state.resolution;
    }
    /**
     * Updates current resolution on panorama load
     */
    __refreshResolution() {
      const resolution = this.resolutions.find((r) => import_core2.utils.deepEqual(this.viewer.config.panorama, r.panorama));
      if (this.state.resolution !== resolution?.id) {
        this.state.resolution = resolution?.id;
        this.settings?.updateButton();
        this.dispatchEvent(new ResolutionChangedEvent(this.state.resolution));
      }
    }
  };
  _ResolutionPlugin.id = "resolution";
  var ResolutionPlugin = _ResolutionPlugin;

  // src/index.ts
  import_core3.DEFAULTS.lang.resolution = "Quality";
  __copyProps(__defProp(exports, "__esModule", { value: true }), src_exports);

}));//# sourceMappingURL=index.js.map